package com.openstackapi.ctds.imp;

import java.util.List;

import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.storage.block.Volume;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.openstack.storage.block.domain.CinderVolume;

import com.openstackapi.ctds.BSController;
import com.openstackapi.ctds.pojo.ResourceID;
import com.openstackapi.ctds.pojo.AttachBSResp;
import com.openstackapi.ctds.pojo.BSParamArray;
import com.openstackapi.ctds.pojo.CreateBSResp;
import com.openstackapi.ctds.pojo.DeleteBSResp;
import com.openstackapi.ctds.pojo.DetachBSResp;
import com.openstackapi.ctds.pojo.QueryBSResp;
import com.openstackapi.ctds.pojo.UpdateBSResp;

public class BSControllerImp implements BSController {

	@Override
	public CreateBSResp CreateBS(String paramFlag, String bSTemplateId, BSParamArray bSParamArray, String resourceID,
			String bSName, String appID, String appName) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		
		Volume vo = new CinderVolume();
				
		vo.toBuilder().name("baater111 Volume")
		        .volumeType("cephtype")
		        .description("baater volume created from java code")
		        .size(1)    //单位为 GB
		        .build();
		
		Volume v = os.blockStorage().volumes().create(vo);

		System.out.println(v); 	
		return null;
	}

	
	
	@Override
	public DeleteBSResp DeleteBS(String bSID) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		// delete specific volume by id
		ActionResponse ar1 = os.blockStorage().volumes().delete(bSID);
		System.out.println("Volume ID = "+ bSID + ", " + ar1.getFault());
		return null;
	}

	@Override
	public QueryBSResp QueryBS(String bSID) {
		// TODO Auto-generated method stub
		return null;
	}
	
	

	@Override
	public UpdateBSResp UpdateBS(String bSID, int VolSize, String bSName) {

		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		
		// list specific volume by id
		Volume volume = os.blockStorage().volumes().get(bSID);   // parameter = bSID
		System.out.println(volume);
		System.out.println(volume.getId());
		
		
		//update block storage name 
		ActionResponse ar1 = os.blockStorage().volumes().update(volume.getId(), bSName, null);
		
		//update block storage size
		ActionResponse ar2 = os.blockStorage().volumes().extend(volume.getId(), VolSize);
		
		
		
		
		
		return null;
	}



	@Override
	public AttachBSResp AttachBS(String bSID, List<ResourceID> resourceID) {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public DetachBSResp DetachBS(String bSID, List<ResourceID> resourceID) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
